package com.car.web.controller.index;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.car.dal.dao.client.TgClientDao;
import com.car.dal.dao.client.TgClientImpDao;
import com.car.dal.dao.department.TgDepartmentDao;
import com.car.dal.dao.office.TgOfficeDao;
import com.car.dal.dao.reception.TgReceptionDao;
import com.car.dal.dao.source.TgSourceDao;
import com.car.dal.dao.sudent.StudentDao;
import com.car.dal.dao.task.TgTaskDao;
import com.car.dal.dao.teastu.TeacherStuDao;
import com.car.dal.dao.user.UserDao;
import com.car.dal.dao.user.UserinfoDao;
import com.car.dal.dao.usergroup.TgUsergroupDao;
import com.car.dal.dao.vote.JsVoteDao;
import com.car.dal.dao.work.TgworkDao;
import com.car.dal.object.JsSpread;
import com.car.dal.object.JsVote;
import com.car.dal.object.Phone;
import com.car.dal.object.StuMsg;
import com.car.dal.object.Teacher;
import com.car.dal.object.TgClient;
import com.car.dal.object.TgClientImp;
import com.car.dal.object.TgReception;
import com.car.dal.object.TgSource;
import com.car.dal.object.TgStudent;
import com.car.dal.object.TgUser;
import com.car.dal.object.TgUserinfo;
import com.car.util.Constants;
import com.car.util.DateUtil;
import com.car.util.JsonExecutor;
import com.car.util.JsonValidator;
import com.car.util.Page;
import com.car.util.PageUtil;
import com.car.util.Result;
import com.car.util.ResultSupport;
import com.car.util.StringUtil;
import com.car.util.UserUtil;
import com.car.util.WebExecutor;
import com.car.util.WriterExcel;
import com.car.util.JsonExecutor.JsonBiz;
import com.car.util.WebExecutor.Biz;
import com.car.web.controller.base.BaseController;
import com.car.web.controller.base.BaseVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 网推部门跟踪
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/network")
public class NetworkController extends BaseController {

	private static final Logger logger = Logger
			.getLogger(NetworkController.class);
    @Resource
    private UserDao userDao;
    @Resource
    private JsVoteDao jsVoteDao;
    @Resource
	private TgworkDao workDao;
    @Resource
    private TgUsergroupDao tgUsergroupDao;
    @Resource
    private TgDepartmentDao tgDepartmentDao;
    @Resource
    private UserinfoDao userinfoDao;
    @Resource
    private TgOfficeDao tgOfficeDao;
	@Resource
	private TgTaskDao stateDao;
	@Resource
	private TgClientDao clientDao;
	@Resource
	private StudentDao studentDao;
	@Resource
	private TgClientImpDao clientImpDao;
	@Resource
	private TgSourceDao sourceDao;
    @Resource
    private TgReceptionDao receptionDao;
    
 	//网推部跟踪
    @RequestMapping(value = "/consu_genz")
	public ModelAndView login(ModelMap modelMap, final BaseVo vo,
			final HttpServletRequest request, final HttpServletResponse response)
			throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");
				String invalid = request.getParameter("invalid");// 有效和无效
				String isBySigned = request.getParameter("isBySigned");// 根据更新时间排序
				String startTime = request.getParameter("startTime");
				String endTime = request.getParameter("endTime");
				String state_s = request.getParameter("state_s");
				String sname = request.getParameter("sname");
				String phone = request.getParameter("phone");
				String province = request.getParameter("province");
				String city = request.getParameter("city");
				String trackings = request.getParameter("trackings");
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				TgClient client = new TgClient();
				if (nickname != null && !"".equals(nickname)
						&& !"null".equals(nickname)) {
					client.setNickname(nickname);
					result.setDefaultModel("nickname", nickname);
				}
				if (invalid != null && !"".equals(invalid)
						&& !"null".equals(invalid) && !"false".equals(invalid)) {
					client.setInvalid(0);
				}
				if(phone!=null && !phone.equals("")){
					client.setPhone(phone);
				}
				if(province!=null && !province.equals("")){
					client.setProvince(province);
				}
				if(city!=null && !city.equals("") && !"null".equals(city)){
					client.setCity(city);
				}else if("null".equals(city)){
					client.setCity("");
				}
				if(trackings!=null && !"".equals(trackings)){
					if(trackings.equals("暂无跟踪信息")){
						client.setTrackings2(Long.valueOf(0));
					}else if(trackings.equals("已有跟踪信息")){
						client.setTrackings1(Long.valueOf(1));
					}
				}
				if (startTime != null && !"".equals(startTime)
						&& !"null".equals(startTime)) {
					try {
						Date date = sdf.parse(startTime);
						client.setStartTime(date.getTime() / 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else{
					try {
						client.setStartTime(sdf.parse("2016-01-01").getTime()/ 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (endTime != null && !"".equals(endTime)
						&& !"null".equals(endTime)) {
					try {
						Date date = sdf.parse(endTime);
						client.setEndTime(date.getTime() / 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				if(sname!=null && !sname.equals("")){
					result.setDefaultModel("sname", sname);
					if(sname.equals("问卷类")){
						sname="网站问卷,手机问卷,19楼问卷,微博问卷，微博粉丝通问卷";
					}else if(sname.equals("商务通类")){
						sname="商务通未知,百度竞价,百度网盟,百度推课,百度信息流,百度教育,百度自然搜索,360搜索,搜狗搜索,商务通神马,百度知道,百度贴吧,商务通留言";
					}else if(sname.equals("报名类")){
						sname="网站报名";
					}else if(sname.equals("商桥类")){
						sname="太优百度竞价，太优百度网盟，太优百度推课，太优商桥未知";
					}else if(sname.equals("站外合作")){
						sname="返学费网，搜学校网，哈哈儿童网，直接访问，教育联展网，远播网，东书房合作，58同城，赶集网";
					}else if(sname.equals("淘宝类")){
						sname="淘宝推广,淘宝咨询";
					}else if(sname.equals("微信类")){
						sname="微信推广";
					}
					client.setSname(sname);
				}else{
					client.setSname("网站问卷,培训世界,手机问卷,19楼问卷,微博问卷,微博粉丝通问卷,商务通未知,百度竞价,百度网盟,百度推课,百度信息流,百度教育,百度自然搜索,360搜索,搜狗搜索,商务通神马,百度知道,百度推广,百度搜索,百度贴吧,微博粉丝通问卷,400电话,离线宝,网站留言,商务通留言,网站报名,商桥留言,百度糯米团购,返学费网,搜学校网,哈哈儿童网,直接访问,教育联展网,远播网,东书房合作,58同城,赶集网,今日头条广告,淘宝推广,淘宝咨询,微信推广");
				}
				//判断客户状态
				if((state_s!=null && !"".equals(state_s))){
					if(state_s.equals("已预约待确认")){
						client.setRenum(1);
						client.setRemin(1);
					}else if(state_s.equals("已预约已到访")){
						client.setRenum(2);
						client.setRemin(2);
					}else if(state_s.equals("已预约未到访")){
						client.setRenum(3);
						client.setRemin(3);
					}else if(state_s.equals("已缴费") ){
						client.setRecenum(6);
						client.setRecemin(6);
					}else if(state_s.equals("已测评")){
						client.setRecenum(5);
						client.setRecemin(1);
					}else if(state_s.equals("无")){
						client.setRenum_n(0);
					}
				}

				
				Integer currPage = 1;
				if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
					currPage = vo.getCurrPage();
				}
				
				if((client.getNickname()!=null && !"".equals(client.getNickname())) || 
						(client.getInvalid()!=null && !"".equals(client.getInvalid())) || 
						(isBySigned!=null && !"".equals(isBySigned)) || 
						(client.getStartTime()!=null && !"".equals(client.getStartTime())) || 
						(client.getEndTime()!=null && !"".equals(client.getEndTime())) || 
						(state_s!=null && !"".equals(state_s)) || 
						(client.getSname()!=null && !"".equals(client.getSname())) ||
						(client.getPhone()!=null && !"".equals(client.getPhone())) ||
						(client.getProvince()!=null && !"".equals(client.getProvince())) ||
						(client.getCity()!=null && !"".equals(client.getCity())) ||
						(client.getTrackings()!=null && !"".equals(client.getTrackings()))){
					Integer total = clientDao.getClientCount(client);
					Page page = PageUtil.createPage(8, currPage, total);
					List<TgClient> list = new ArrayList<TgClient>();
					if (isBySigned!=null && isBySigned.equals("cdate")) {
						// 根据创建时间排序
						list = clientDao.getClientByTypeCdate(client, page);
					} else {
						// 根据更新时间排序
						list = clientDao.getTgClientByType(client, page);
					}
					try{
						
					if (list != null && list.size() > 0) {
						for (TgClient c : list) {
							// 时间和备注内容
								if (c.getMdate() != null
										&& !"".equals(c.getMdate())) {
									String date = sdf2.format(new Date(c.getMdate() * 1000));
									c.setModeTime(date);
								}
								
								if (c.getCdate() != null
										&& !"".equals(c.getCdate())) {
									String date = sdf2.format(new Date(c.getCdate() * 1000));
									c.setDateTime(date);
								}
								//获取被分配老师
								if(c.getTo_username()!=null && !"".equals(c.getTo_username())){
									TgUserinfo info = new TgUserinfo();
									info.setUsername(c.getTo_username());
									List<TgUserinfo> infoList = userinfoDao.findByExample(info);
									if(infoList!=null && infoList.size()>0){
										String nick = infoList.get(0).getNickname();
										c.setToname(nick);
									}
								}
							//客户状态	
								String stat="";
								if(c.getState()!=null && !"".equals(c.getState())){
									int renum = c.getState();
									if(renum == 1 ){
										stat = "已预约待确认";
									}else if(renum==2){
										stat = "已预约已到访";
									}else if(renum==3){
										stat = "已预约未到访";
									}
									c.setStates(stat);
								}
							
							if(c.getWorth()!=null && !"".equals(c.getWorth())){
								Integer recenum =c.getWorth();
								if(recenum == 6){
									if(stat!=""){
										stat =stat+","+"已缴费";
									}else{
										stat ="已缴费";
									}
								}else if(0<recenum  && recenum <=5){
									if(stat!=""){
										stat =stat+","+"已测评";
									}else{
										stat ="已测评";
									}
								}
								c.setStates(stat);
							}
							
							// 最后更新信息和状态和总更新数量
							if (c.getLastdate() != null && !"".equals(c.getLastdate())) {
								String createTime = sdf2.format(new Date(c.getLastdate() * 1000));
								c.setNewTime(createTime);
							}
							
							if ( c.getLast_track() != null && !"".equals( c.getLast_track())) {
								c.setLast_track(c.getLast_track().replace("&nbsp;","").trim());
								if(StringUtil.isNum(c.getLast_track())){
									c.setFallow(c.getLast_track());
								}else{
								boolean flag = StringUtil.getHanzi(c.getLast_track().replaceAll("</?[^<]+>", ""));
								if (!flag) {
										byte[] bytes = StringUtil.hexDecode(c.getLast_track());
										String ObjContent = new String(bytes,"UTF-8");
										c.setFallow(ObjContent);
								}else {
									c.setFallow(c.getLast_track());
									}
								}
								if(c.getFallow().contains("\"")){
									c.setFallow(c.getFallow().replace('"',' '));
								}
							} 
							
							if(c.getTrackings() != null && !"".equals(c.getTrackings())){
								c.setTracksize(String.valueOf(c.getTrackings()));
							}
							 Gson gson = new Gson();
							if (c.getPhone() != null && !"".equals(c.getPhone())) {
	                            if (c.getPhone().contains("[{")) {
	                                List<Phone> phone1 = gson.fromJson(c.getPhone(),
	                                        new TypeToken<List<Phone>>() {
	                                        }.getType());
	                                c.setPhlist(phone1);
//								c.setPhone(c.getPhlist().get(0).getPhone());
	                            }
	                        }
							
						}
					}
					}catch (Exception e) {
						e.printStackTrace();
					}
					
					result.setDefaultModel("client", client);
					result.setDefaultModel("startTime", startTime);
					result.setDefaultModel("endTime", endTime);
					result.setDefaultModel("invalid", invalid);
					result.setDefaultModel("state_s", state_s);
					result.setDefaultModel("trackings", trackings);
					result.setDefaultModel("page", page);
					result.setDefaultModel("list", list);
				}
				return result;
			}
		});
		return new ModelAndView("network/consultant_list", modelMap);
	}
    
    
    //市场部跟踪
    @RequestMapping(value = "/shic_genz")
	public ModelAndView shic_genz(ModelMap modelMap, final BaseVo vo,
			final HttpServletRequest request, final HttpServletResponse response)
			throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String nickname = request.getParameter("nickname");
				String invalid = request.getParameter("invalid");// 有效和无效
				String isBySigned = request.getParameter("isBySigned");// 根据更新时间排序
				String startTime = request.getParameter("startTime");
				String endTime = request.getParameter("endTime");
				String state_s = request.getParameter("state_s");
				String sname = request.getParameter("sname");
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				TgClient client = new TgClient();
				if (nickname != null && !"".equals(nickname)
						&& !"null".equals(nickname)) {
					client.setNickname(nickname);
					result.setDefaultModel("nickname", nickname);
				}
				if (invalid != null && !"".equals(invalid)
						&& !"null".equals(invalid) && !"false".equals(invalid)) {
					client.setInvalid(0);
				}
				if (startTime != null && !"".equals(startTime)
						&& !"null".equals(startTime)) {
					try {
						Date date = sdf.parse(startTime);
						client.setStartTime(date.getTime() / 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else{
					try {
						client.setStartTime(sdf.parse("2016-01-01").getTime()/ 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (endTime != null && !"".equals(endTime)
						&& !"null".equals(endTime)) {
					try {
						Date date = sdf.parse(endTime);
						client.setEndTime(date.getTime() / 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				if(sname!=null && !sname.equals("")){
					result.setDefaultModel("sname", sname);
					client.setSname(sname);
					if("市场部推广".equals(sname)){
						client.setSname("市场部推广(讲座),市场部推广(地推),市场部推广(交换资源)");
					}
				}else{
					client.setSname("公益讲座,市场部推广(讲座),市场部推广(地推),市场部推广(交换资源),传单派发,市场部问卷,公益微课");
				}
				//判断客户状态
				if((state_s!=null && !"".equals(state_s))){
					if(state_s.equals("已预约")){
						client.setRenum(2);
						client.setRemin(1);
					}else if(state_s.equals("未出现")){
						client.setRenum(3);
						client.setRemin(3);
					}else if(state_s.equals("已缴费") ){
						client.setRecenum(6);
						client.setRecemin(6);
					}else if(state_s.equals("已测评")){
						client.setRecenum(5);
						client.setRecemin(1);
					}
				}


				Integer currPage = 1;
				if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
					currPage = vo.getCurrPage();
				}
				
				Integer total = clientDao.getClientCount5(client);
				Page page = PageUtil.createPage(8, currPage, total);
				List<TgClient> list = new ArrayList<TgClient>();
				if (isBySigned!=null && isBySigned.equals("cdate")) {
					// 根据创建时间排序
					list = clientDao.getClientByTypeCdate(client, page);
				} else {
					// 根据更新时间排序
					list = clientDao.getTgClientByType5(client, page);
				}
				try{
					
				if (list != null && list.size() > 0) {
					for (TgClient c : list) {
						// 时间和备注内容
							if (c.getMdate() != null
									&& !"".equals(c.getMdate())) {
								String date = sdf2.format(new Date(c.getMdate() * 1000));
								c.setModeTime(date);
							}
							
							if (c.getCdate() != null
									&& !"".equals(c.getCdate())) {
								String date = sdf2.format(new Date(c.getCdate() * 1000));
								c.setDateTime(date);
							}
							
						//客户状态
						String stat="";
						if(c.getState()!=null && !"".equals(c.getState())){
							int renum = c.getState();
							if(renum >= 1 && renum <= 2){
								stat = "已预约";
							}else if(renum==3){
								stat = "未出现";
							}
							c.setStates(stat);
						}
						
						if(c.getWorth()!=null && !"".equals(c.getWorth())){
							Integer recenum =c.getWorth();
							if(recenum == 6){
								if(stat!=""){
									stat =stat+","+"已缴费";
								}else{
									stat ="已缴费";
								}
							}else if(0<recenum  && recenum <=5){
								if(stat!=""){
									stat =stat+","+"已测评";
								}else{
									stat ="已测评";
								}
							}
							c.setStates(stat);
						}
						
						// 最后更新信息和状态和总更新数量
						if (c.getLastdate() != null && !"".equals(c.getLastdate())) {
							String createTime = sdf2.format(new Date(c.getLastdate() * 1000));
							c.setNewTime(createTime);
						}
						
						if ( c.getLast_track() != null && !"".equals( c.getLast_track())) {
							c.setLast_track(c.getLast_track().replace("&nbsp;","").trim());
							if(StringUtil.isNum(c.getLast_track())){
								c.setFallow(c.getLast_track());
							}else{
							boolean flag = StringUtil.getHanzi(c.getLast_track().replaceAll("</?[^<]+>", ""));
							if (!flag) {
									byte[] bytes = StringUtil.hexDecode(c.getLast_track());
									String ObjContent = new String(bytes,"UTF-8");
									c.setFallow(ObjContent);
							}else {
								c.setFallow(c.getLast_track());
								}
							}
							if(c.getFallow().contains("\"")){
								c.setFallow(c.getFallow().replace('"',' '));
							}
						} 
						
						if(c.getTrackings() != null && !"".equals(c.getTrackings())){
							c.setTracksize(String.valueOf(c.getTrackings()));
						}
					}
				}
				}catch (Exception e) {
					e.printStackTrace();
				}
				
				result.setDefaultModel("startTime", startTime);
				result.setDefaultModel("endTime", endTime);
				result.setDefaultModel("invalid", invalid);
				result.setDefaultModel("state_s", state_s);
				result.setDefaultModel("page", page);
				result.setDefaultModel("list", list);
				return result;
			}
		});
		return new ModelAndView("network/consultant_sc_list", modelMap);
	}
    
    
    // 导入客户资源列表，除离线宝、百度糯米和今日头条
 	@RequestMapping(value = "/consultant_import_list2")
 	public ModelAndView consultant_import_list(ModelMap modelMap,
 			final BaseVo vo, final HttpServletRequest request,
 			final HttpServletResponse response) throws Exception {
 		modelMap = WebExecutor.execute(logger, vo, new Biz() {
 			public Result process() {
 				Result result = new ResultSupport(true);
 				try {
 					TgClientImp clientImp = new TgClientImp();
 					String type = request.getParameter("type");
 					String lianxi_name = request.getParameter("lianxi_name");
 					String phone = request.getParameter("phone");
 					String state = request.getParameter("state");
 					String st_type = request.getParameter("st_type");
 					String color = request.getParameter("color");
 					String state1 = request.getParameter("stat");
 					String teacherName = request.getParameter("teacherName");
 					String stardate = request.getParameter("stardate");
 					String enddate = request.getParameter("enddate");
 					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 					
 					TgUser user = UserUtil.getUserFromCookies();
 					if (user == null) {
 						return result;
 					}
 					
 					if(type!=null && !type.equals("")){
 						clientImp.setType(type);
 					}
 					
 					if(user.getUsergroup()==2){
 						clientImp.setUsername(user.getUsername());
 					}
 					
 					if(color!=null && !color.equals("")){
 						clientImp.setColor("1");
 					}
 					
 					if(st_type!=null && !st_type.equals("")){
 						clientImp.setSt_type(Integer.valueOf(st_type));
 					}
 					
 					if(stardate!=null && !stardate.equals("")){
 						clientImp.setStartTime(stardate);
 					}
 					
 					if(enddate!=null && !enddate.equals("")){
 						clientImp.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(enddate))));
 					}
 					//已分配老师下拉传过来的字符串
					if(teacherName != null && !"".equals(teacherName)){
						clientImp.setTo_nickname(teacherName);
					}
 					//判断客户状态
 					if((state1!=null && !"".equals(state1))){
 						result.setDefaultModel("state1", state1);
 						if((state1.equals("已预约"))){
 							clientImp.setRenum(2);
 							clientImp.setRemin(1);
 						}else if(state1.equals("未出现") ){
 							clientImp.setRenum(3);
 							clientImp.setRemin(3);
 						}else if(state1.equals("已缴费")){
 							clientImp.setRecenum(6);
 							clientImp.setRecemin(6);
 						}else if(state1.equals("已测评")){
 							clientImp.setRecenum(5);
 							clientImp.setRecemin(1);
 						}
 					}
 					
 					clientImp.setLianxi_name(lianxi_name);
 					clientImp.setPhone(phone);
 					clientImp.setState(state);
 					/*if(!user.getKey().equals("竞思总部") && !user.getUsername().equals("he")){
 						clientImp.setJob2(user.getKey());
 					}*/
 	
 					Integer currPage = 1;
 					if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
 						currPage = vo.getCurrPage();
 					}
 					
 					Integer total = clientImpDao.getClientListImpCByType2(clientImp);
 					Page page = PageUtil.createPage(8, currPage, total);
 					List<TgClientImp> list = clientImpDao.getClientListImpByType2(clientImp, page);
 					List<TgSource> sourcelist = sourceDao.findAll(TgSource.class);
 					
 					result.setDefaultModel("sourcelist", sourcelist);
 					result.setDefaultModel("list", list);
 					result.setDefaultModel("page", page);
 					result.setDefaultModel("type", type);
 					result.setDefaultModel("clientImp", clientImp);
 					result.setDefaultModel("currPage", currPage);
 					result.setDefaultModel("stardate", stardate);
 					result.setDefaultModel("enddate", enddate);
 					result.setDefaultModel("teacherName",teacherName);
 				} catch (Exception e) {
 					e.printStackTrace();
 				}
 				return result;
 			}
 		});

 		return new ModelAndView("network/consultant_import_list2", modelMap);
 	}
 	
    /**
     * @Description:市场部导入资源分配
     * @author: Hqd
     * @date: 2017年11月21日 下午3:58:06 
     * @return
     */
 	@RequestMapping(value = "/sczy_allot_list")
 	public ModelAndView sczy_allot_list(ModelMap modelMap,final BaseVo vo, final HttpServletRequest request,
 			final HttpServletResponse response) throws Exception {
 		modelMap = WebExecutor.execute(logger, vo, new Biz() {
 			public Result process() {
 				Result result = new ResultSupport(true);
 				try {
 					TgClientImp clientImp = new TgClientImp();
 					String type = request.getParameter("type");
 					String lianxi_name = request.getParameter("lianxi_name");
 					String phone = request.getParameter("phone");
 					String state = request.getParameter("state");
 					String st_type = request.getParameter("st_type");
 					String color = request.getParameter("color");
 					String state1 = request.getParameter("stat");
 					String stardate = request.getParameter("stardate");
 					String enddate = request.getParameter("enddate");
 					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 					
 					TgUser user = UserUtil.getUserFromCookies();
 					if (user == null) {
 						return result;
 					}
 					
 					if(type!=null && !type.equals("")){
 						clientImp.setType(type);
 					}else{
 						String type2 = "SCBTGJHZY13,ct5yq4jhc6c7,SCBTGDT5515";
 						clientImp.setType2(type2);
 					}
 					
 					if(user.getUsergroup()==2){
 						clientImp.setUsername(user.getUsername());
 					}
 					if(user.getDepartments().equals("课程咨询部")||user.getNickname().equals("郭斌")||user.getNickname().equals("毛怡敏")||user.getNickname().equals("王星宇")||user.getNickname().equals("张建")) {
 						clientImp.setTui_username("");
 					}else {
 						clientImp.setTui_username(user.getUsername());
 					}
 					if(color!=null && !color.equals("")){
 						clientImp.setColor("1");
 					}
 					
 					if(st_type!=null && !st_type.equals("")){
 						clientImp.setSt_type(Integer.valueOf(st_type));
 					}
 					
 					if(stardate!=null && !stardate.equals("")){
 						clientImp.setStartTime(stardate);
 					}
 					
 					if(enddate!=null && !enddate.equals("")){
 						clientImp.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(enddate))));
 					}
 					
 					//判断客户状态
 					if((state1!=null && !"".equals(state1))){
 						result.setDefaultModel("state1", state1);
 						if((state1.equals("已预约"))){
 							clientImp.setRenum(2);
 							clientImp.setRemin(1);
 						}else if(state1.equals("未出现") ){
 							clientImp.setRenum(3);
 							clientImp.setRemin(3);
 						}else if(state1.equals("已缴费")){
 							clientImp.setRecenum(6);
 							clientImp.setRecemin(6);
 						}else if(state1.equals("已测评")){
 							clientImp.setRecenum(5);
 							clientImp.setRecemin(1);
 						}
 					}
 					
 					clientImp.setLianxi_name(lianxi_name);
 					clientImp.setPhone(phone);
 					clientImp.setState(state);
 					Integer currPage = 1;
 					if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
 						currPage = vo.getCurrPage();
 					}
 					
 					Integer total = clientImpDao.getClientListImpCByType3(clientImp);
 					Page page = PageUtil.createPage(50, currPage, total);
 					List<TgClientImp> list = clientImpDao.getClientListImpByType3(clientImp, page);
 					List<TgSource> sourcelist = sourceDao.findAll(TgSource.class);
 					
 					result.setDefaultModel("sourcelist", sourcelist);
 					result.setDefaultModel("list", list);
 					result.setDefaultModel("page", page);
 					result.setDefaultModel("type", clientImp.getType());
 					result.setDefaultModel("clientImp", clientImp);
 					result.setDefaultModel("currPage", currPage);
 					result.setDefaultModel("stardate", stardate);
 					result.setDefaultModel("enddate", enddate);
 				} catch (Exception e) {
 					e.printStackTrace();
 				}
 				return result;
 			}
 		});

 		return new ModelAndView("network/sczy_allot_list", modelMap);
 	}
  
 	/**
 	 * @Description:市场部资源批量分配
 	 * @author: Hqd
 	 * @date: 2017年11月21日 下午4:58:57 
 	 * @return
 	 */
 	@RequestMapping(value = "/sczy_plfp")
 	public ModelAndView sczy_plfp(ModelMap modelMap, final LoginVo loginVo, final HttpServletRequest request,
 			final HttpServletResponse response) throws Exception {
 		modelMap = WebExecutor.execute(logger, loginVo, new Biz() {
 			public Result process() {
 				Result result = new ResultSupport(true);
 				String ids = request.getParameter("ids");
 				String[] idArr = ids.split(",");
 				List<JsVote> jsVotes = new ArrayList<JsVote>();
 				for(String id : idArr){
 					JsVote jsVote = jsVoteDao.findById(JsVote.class, Long.parseLong(id));
 					if(jsVote != null){
 						jsVotes.add(jsVote);
 					}
 				}
 				TgUserinfo user  = new TgUserinfo();
   				user.setJob(Constants.JOB_1);
   				List<TgUserinfo> uList_1 =userinfoDao.getUserInfoByUserInfo(user);
   				user.setJob(Constants.JOB_2);
   				List<TgUserinfo> uList_2 =userinfoDao.getUserInfoByUserInfo(user);
   				result.setDefaultModel("uList_1", uList_1);
   				result.setDefaultModel("uList_2", uList_2);
 				result.setDefaultModel("ids", ids);
 				result.setDefaultModel("jsVotes", jsVotes);
 				return result;
 			}
 		});
 		return new ModelAndView("network/sczy_plfp", modelMap);
 	}
 	
 	//网推资源跟踪信息下载
 	@RequestMapping(value = "/consu_genzXZ")
 	public ModelAndView consu_genzXZ(ModelMap modelMap, final BaseVo vo,
			final HttpServletRequest request, final HttpServletResponse response)
			throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String invalid = request.getParameter("invalid");// 有效和无效
				String startTime = request.getParameter("startTime");
				String endTime = request.getParameter("endTime");
				String phone = request.getParameter("phone");
				String province = request.getParameter("province");
				String trackings = request.getParameter("trackings");
				String isBySigned = request.getParameter("isBySigned");// 根据更新时间排序
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				TgClient client = new TgClient();
				if (invalid != null && !"".equals(invalid)
						&& !"null".equals(invalid) && !"false".equals(invalid)) {
					client.setInvalid(0);
				}
				if(phone!=null && !phone.equals("")){
					client.setPhone(phone);
				}
				if(province!=null && !province.equals("")){
					client.setProvince(province);
				}
				if(trackings!=null && !"".equals(trackings)){
					if(trackings.equals("暂无跟踪信息")){
						client.setTrackings2(Long.valueOf(0));
					}else if(trackings.equals("已有跟踪信息")){
						client.setTrackings1(Long.valueOf(1));
					}
				}
				if (startTime != null && !"".equals(startTime)
						&& !"null".equals(startTime)) {
					try {
						Date date = sdf.parse(startTime);
						client.setStartTime(date.getTime() / 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else{
					try {
						client.setStartTime(sdf.parse("2016-01-01").getTime()/ 1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (endTime != null && !"".equals(endTime)
						&& !"null".equals(endTime)) {
					try {
						Date date = sdf.parse(endTime);
						client.setEndTime(date.getTime() / 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				
				Integer currPage = 1;
				if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
					currPage = vo.getCurrPage();
				}

				Integer total = clientDao.getClientCount(client);
				Page page = PageUtil.createPage(100, currPage, total);
				List<TgClient> list = new ArrayList<TgClient>();
				if (isBySigned!=null && isBySigned.equals("cdate")) {
					// 根据创建时间排序
					list = clientDao.getClientByTypeCdate(client, page);
				} else {
					// 根据更新时间排序
					list = clientDao.getTgClientByType(client, page);
				}
				try{
					
					if (list != null && list.size() > 0) {
						for (TgClient c : list) {
							// 时间和备注内容
								if (c.getMdate() != null
										&& !"".equals(c.getMdate())) {
									String date = sdf2.format(new Date(c.getMdate() * 1000));
									c.setModeTime(date);
								}
								
								if (c.getCdate() != null
										&& !"".equals(c.getCdate())) {
									String date = sdf2.format(new Date(c.getCdate() * 1000));
									c.setDateTime(date);
								}
								
							//客户状态
							String stat="";
							if(c.getState()!=null && !"".equals(c.getState())){
								int renum = c.getState();
								if(renum >= 1 && renum <= 2){
									stat = "已预约";
								}else if(renum==3){
									stat = "未出现";
								}
								c.setStates(stat);
							}
							
							if(c.getWorth()!=null && !"".equals(c.getWorth())){
								Integer recenum =c.getWorth();
								if(recenum == 6){
									if(stat!=""){
										stat =stat+","+"已缴费";
									}else{
										stat ="已缴费";
									}
								}else if(0<recenum  && recenum <=5){
									if(stat!=""){
										stat =stat+","+"已测评";
									}else{
										stat ="已测评";
									}
								}
								c.setStates(stat);
							}
							
							// 最后更新信息和状态和总更新数量
							if (c.getLastdate() != null && !"".equals(c.getLastdate())) {
								String createTime = sdf2.format(new Date(c.getLastdate() * 1000));
								c.setNewTime(createTime);
							}
							
							if ( c.getLast_track() != null && !"".equals( c.getLast_track())) {
								c.setLast_track(c.getLast_track().replace("&nbsp;","").trim());
								if(StringUtil.isNum(c.getLast_track())){
									c.setFallow(c.getLast_track());
								}else{
								boolean flag = StringUtil.getHanzi(c.getLast_track().replaceAll("</?[^<]+>", ""));
								if (!flag) {
										byte[] bytes = StringUtil.hexDecode(c.getLast_track());
										String ObjContent = new String(bytes,"UTF-8");
										c.setFallow(ObjContent);
								}else {
									c.setFallow(c.getLast_track());
									}
								}
								if(c.getFallow().contains("\"")){
									c.setFallow(c.getFallow().replace('"',' '));
								}
							} 
							
							if(c.getTrackings() != null && !"".equals(c.getTrackings())){
								c.setTracksize(String.valueOf(c.getTrackings()));
							}
							 Gson gson = new Gson();
							if (c.getPhone() != null && !"".equals(c.getPhone())) {
	                            if (c.getPhone().contains("[{")) {
	                                List<Phone> phone1 = gson.fromJson(c.getPhone(),
	                                        new TypeToken<List<Phone>>() {
	                                        }.getType());
	                                c.setPhlist(phone1);
	//							c.setPhone(c.getPhlist().get(0).getPhone());
	                            }
	                        }
							
						}
					}
					String name = WriterExcel.writeExcelwltgXz(list, startTime, endTime);
  					result.setDefaultModel("name", name);
				}catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return new ModelAndView("", modelMap);
 	}
 	
}
